package com.anche.cywebnew.Service.index;

import com.anche.cywebnew.DAO.index.IndexMapper;
import com.anche.cywebnew.DAO.systemManager.UserMapper;
import com.anche.cywebnew.DTO.CurrLoginInfo;
import com.anche.cywebnew.Pojo.systemManager.SysUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;


    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Integer> getBussinessTrend(String organ) {
        List<Integer> integerList1 = new ArrayList<>();
        int count = 0;
        List<Map<String, String>> idNameMaps = indexMapper.getCyqTimeBussiness(organ);
        for (int i = 0; i < idNameMaps.size(); i++) {
            if (i < 16) {
                count += Integer.parseInt(String.valueOf(idNameMaps.get(i).get("NAME")));
            } else if (i == 16) {
                count += Integer.parseInt(String.valueOf(idNameMaps.get(i).get("NAME")));
                integerList1.add(count);
            } else if (i >= 39 && i < 48) {
                count = 0;
                count += Integer.parseInt(String.valueOf(idNameMaps.get(i).get("NAME")));
            } else if (i == 48) {
                count += Integer.parseInt(String.valueOf(idNameMaps.get(i).get("NAME")));
                integerList1.add(count);
            } else {
                integerList1.add(Integer.valueOf(String.valueOf(idNameMaps.get(i).get("NAME"))));
            }
        }
        return integerList1;
    }

    @Override
    public List<Map<String, String>> getBussinessSpread(String organ) {
        List<Map<String, String>> mapList = new ArrayList<>();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String currDate = format.format(date);
        List<Map<String, String>> ywTotalMaps = indexMapper.getBussinessSpread(currDate, organ);
        for (int i = 0; i < ywTotalMaps.size(); i++) {
            Map<String, String> pieChart = new HashMap<>();
            String ywlx = ywTotalMaps.get(i).get("HPZL");
            String total = String.valueOf(ywTotalMaps.get(i).get("TOTAL"));
            switch (ywlx) {
                case "01":
                    pieChart.put("name", "大型汽车");
                    pieChart.put("value", total);
                    break;
                case "02":
                    pieChart.put("name", "小型汽车");
                    pieChart.put("value", total);
                    break;
                case "51":
                    pieChart.put("name", "大型新能源");
                    pieChart.put("value", total);
                    break;
                case "52":
                    pieChart.put("name", "小型新能源");
                    pieChart.put("value", total);
                    break;
                case "23":
                    pieChart.put("name", "警用汽车");
                    pieChart.put("value", total);
                    break;
                case "07":
                    pieChart.put("name", "普通摩托车");
                    pieChart.put("valuet", total);
                    break;
                default:
                    pieChart.put("name", "其他");
                    pieChart.put("value", total);
                    break;
            }
            mapList.add(pieChart);
        }
        return mapList;
    }

    @Override
    public List<Integer> getLastSevenBus(String organ) {
        List<String> arr = getLastSevenDay1();
        List<Integer> busList = new ArrayList<>();
        for (int i = 0; i < arr.size(); i++) {
            int count = indexMapper.getBusCountByDay(arr.get(i), organ);
            busList.add(count);
        }
        return busList;
    }

    @Override
    public List<String> getLastSevenDay() {
        List<String> arr = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        Calendar c = null;
        for (int i = 7; i > 0; i--) {
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -i);
            arr.add(sdf.format(c.getTime()));
        }
        return arr;
    }

    @Override
    public CurrLoginInfo getCurrUserInfo() throws Exception {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        CurrLoginInfo currLoginInfo = new CurrLoginInfo();
        user = userMapper.getUserByLoginName(user.getLoginname());
        currLoginInfo.setZhyxq(user.getZhyxq());
        long zhDay = (user.getZhyxq().getTime() - new Date().getTime()) / (24 * 60 * 60 * 1000);
        currLoginInfo.setZhyxqdays(zhDay);
        currLoginInfo.setMmyxq(user.getMmyxq());
        long mmDay = (user.getMmyxq().getTime() - new Date().getTime()) / (24 * 60 * 60 * 1000);
        currLoginInfo.setMmyxqdays(mmDay);
        currLoginInfo.setScdlip(user.getScdlip());
        currLoginInfo.setScdlsj(user.getScdlsj());
        InetAddress inetAddress = InetAddress.getLocalHost();
        String ip = inetAddress.getHostAddress();//获得本机Ip
        currLoginInfo.setZjdlip(ip);
        currLoginInfo.setZjdlsj(new Date());
        return currLoginInfo;
    }

    @Override
    public float getDownloadPercent(String organ) {
        int total = indexMapper.getAllVideoCountToday(organ);
        int download = indexMapper.getDownloadVideoCountToday(organ);
        if (total != 0) {
            float percent = (float) download / total;
            BigDecimal bg = new BigDecimal(percent).setScale(2, RoundingMode.UP);
            return bg.floatValue();
        } else {
            return 0;
        }
    }

    @Override
    public float getSuccessPercent(String organ) {
        int total = indexMapper.getDownloadVideoCountToday(organ);
        int retire = indexMapper.getSuccessToday(organ);
        if (total != 0) {
            float percent = (float) retire / total;
            BigDecimal bg = new BigDecimal(percent).setScale(2, RoundingMode.UP);
            return bg.floatValue();
        } else {
            return 0;
        }
    }


    public List<String> getLastSevenDay1() {
        List<String> arr = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        Calendar c = null;
        for (int i = 7; i > 0; i--) {
            c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -i);
            arr.add(sdf.format(c.getTime()));
        }
        return arr;
    }
}
